/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TCPserver.app;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author emanuellima
 */
public class ServerTCP implements Runnable {
    
    private DataOutputStream outstream; // Cria um canal para enviar Dados
    private DataInputStream instream;   // Cria um canal para receber Dados
    private int port = 8080;            // Porta usada
    private ServerSocket serverSocket;
    private Socket socket;
    private ArrayList listClients;      // Lista de clientes ON
    private boolean listening = true;
    
    public ServerTCP(){
        
    }

    @Override
    public void run() {
        
        while(listening){
            try {
                
                serverSocket = new ServerSocket(port);
                System.out.println("Porta 8080 aberta!");
                System.out.println("Aguardando um conexão cliente");  
                socket = serverSocket.accept();
                System.out.println("Nova conexão com o cliente " +
                socket.getInetAddress().getHostAddress());
                outstream = new DataOutputStream(socket.getOutputStream());  
                instream = new DataInputStream(socket.getInputStream());
            
            } catch (IOException ex) {
                Logger.getLogger(ServerTCP.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    
    public ArrayList sendListClients()  {
        
        return listClients;
    }
            
    
}
